<template>
  <view class="ratings-container">
    <!-- 标题栏 -->
    <view class="header">
      <text class="title">我的评分</text>
      <text class="count">共{{ total }}个</text>
    </view>

    <!-- 评分列表 -->
    <view class="ratings-list" v-if="ratingList.length > 0">
      <view 
        class="rating-item" 
        v-for="item in ratingList" 
        :key="item.id"
        @click="viewDishDetail(item.dishId)"
      >
        <image :src="item.dishImage || '/static/images/default-dish.jpg'" class="dish-image" mode="aspectFill" />
        <view class="dish-info">
          <text class="dish-name">{{ item.dishName || '菜品' + item.dishId }}</text>
          <view class="rating-info">
            <uni-rate :value="item.rating" :readonly="true" :size="16" />
            <text class="rating-text">{{ item.rating }}分</text>
          </view>
          <text class="rating-time">评分时间: {{ formatTime(item.createTime) }}</text>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view class="empty-state" v-else>
      <text class="empty-text">暂无评分记录</text>
      <button class="goto-recommend" @click="gotoRecommend">
        去推荐页面看看
      </button>
    </view>

    <!-- 加载更多 -->
    <uni-load-more :status="loadStatus" v-if="ratingList.length > 0" />
  </view>
</template>

<script>
import { getUserRatingList } from '@/api/diet'

export default {
  data() {
    return {
      ratingList: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      loadStatus: 'more'
    }
  },
  onLoad() {
    this.loadRatingList()
  },
  onReachBottom() {
    if (this.loadStatus === 'more') {
      this.loadMore()
    }
  },
  methods: {
    async loadRatingList() {
      try {
        this.loadStatus = 'loading'
        const userId = this.getUserId()
        
        const res = await getUserRatingList(userId, this.pageNum, this.pageSize)
        
        if (res.code === 200) {
          if (this.pageNum === 1) {
            this.ratingList = res.rows || []
          } else {
            this.ratingList = [...this.ratingList, ...(res.rows || [])]
          }
          this.total = res.total || 0
          
          if (res.rows && res.rows.length < this.pageSize) {
            this.loadStatus = 'noMore'
          } else {
            this.loadStatus = 'more'
          }
        } else {
          this.loadStatus = 'more'
          uni.showToast({ title: '加载失败', icon: 'none' })
        }
      } catch (error) {
        this.loadStatus = 'more'
        uni.showToast({ title: '加载失败', icon: 'none' })
      }
    },
    
    loadMore() {
      this.pageNum++
      this.loadRatingList()
    },
    
    getUserId() {
      let userId = uni.getStorageSync('userId')
      if (!userId) {
        userId = 100
        uni.setStorageSync('userId', userId)
      }
      return Number(userId)
    },
    
    formatTime(timeStr) {
      if (!timeStr) return ''
      const date = new Date(timeStr)
      return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
    },
    
    viewDishDetail(dishId) {
      uni.navigateTo({
        url: `/pages/diet/detail/index?id=${dishId}`
      })
    },
    
    gotoRecommend() {
      uni.switchTab({
        url: '/pages/diet/recommend/index'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.ratings-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
  padding: 20rpx;
  background: white;
  border-radius: 16rpx;
}

.title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.count {
  font-size: 24rpx;
  color: #666;
}

.rating-item {
  display: flex;
  align-items: center;
  background: white;
  border-radius: 16rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
}

.dish-image {
  width: 120rpx;
  height: 120rpx;
  border-radius: 12rpx;
  margin-right: 20rpx;
}

.dish-info {
  flex: 1;
}

.dish-name {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 10rpx;
}

.rating-info {
  display: flex;
  align-items: center;
  margin-bottom: 10rpx;
}

.rating-text {
  font-size: 24rpx;
  color: #faad14;
  margin-left: 10rpx;
  font-weight: bold;
}

.rating-time {
  font-size: 24rpx;
  color: #999;
}

.empty-state {
  text-align: center;
  padding: 100rpx 0;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
  display: block;
  margin-bottom: 40rpx;
}

.goto-recommend {
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 30rpx;
  padding: 20rpx 40rpx;
  font-size: 28rpx;
}
</style>